//
//  AlbumPersistence.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/23.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "SecretAlbumsPersistence.h"

@implementation SecretAlbumsPersistence

static SecretAlbumsPersistence *sharedManager = nil;
+ (instancetype)sharedManager
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sharedManager = [SecretAlbumsPersistence new];
    });
    
    return sharedManager;
}

#pragma mark - 公有方法

/** 创建albumId对应的文件目录，包括子目录Thumbnail、AspectRatioThumbnail、Media */
- (void)createDirectoryAtAlbumID:(NSString *)albumID
{
    if ([self isExistDirectoryAtAlbumID:albumID]) return;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createDirectoryAtPath:[self applicationDocumentsDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self thumbnailDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self aspectRatioThumbnailDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self mediaDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
}

/** 创建albumId对应的Thumbnail文件 */
- (void)createThumbnailAtAlbumID:(NSString *)albumID name:(NSString *)name data:(NSData *)data
{
    if (![self isExistThumbnailDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self thumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    [data writeToFile:path atomically:YES];
}

/** 创建albumId对应的AspectRatioThumbnail文件 */
- (void)createAspectRatioThumbnailAtAlbumID:(NSString *)albumID name:(NSString *)name data:(NSData *)data
{
    if (![self isExistAspectRatioThumbnailDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self aspectRatioThumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    [data writeToFile:path atomically:YES];
}

/** 创建albumId对应的Media文件 */
- (void)createMediaAtAlbumID:(NSString *)albumID name:(NSString *)name data:(NSData *)data
{
    if (![self isExistMediaDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self mediaDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]) {
        [fileManager createFileAtPath:path contents:nil attributes:nil];
    }
    
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    if (!fileHandle) return;
    
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:data];
    
    [fileHandle closeFile];
}

/** 返回albumId对应的Thumbnail目录 */
- (NSString *)thumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Thumbnail"];
    
    return path;
}

/** 返回albumId对应的AspectRatioThumbnail目录 */
- (NSString *)aspectRatioThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"AspectRatioThumbnail"];
    
    return path;
}

/** 返回albumId对应的Media目录 */
- (NSString *)mediaDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Media"];
    
    return path;
}

/** 删除albumId对应的Thumbnail文件 */
- (void)removeThumbnailAtAlbumID:(NSString *)albumID name:(NSString *)name
{
    if (![self isExistThumbnailDirectoryAtAlbumID:albumID]
        || 0 == name.length){
        return;
    }
    
    NSString *directoryPath = [self thumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
}

/** 删除albumId对应的AspectRatioThumbnail文件 */
- (void)removeAspectRatioThumbnailAtAlbumID:(NSString *)albumID name:(NSString *)name
{
    if (![self isExistAspectRatioThumbnailDirectoryAtAlbumID:albumID]
        || 0 == name.length){
        return;
    }
    
    NSString *directoryPath = [self aspectRatioThumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
}

/** 删除albumId对应的Media文件 */
- (void)removeMediaAtAlbumID:(NSString *)albumID name:(NSString *)name
{
    if (![self isExistMediaDirectoryAtAlbumID:albumID]
        || 0 == name.length){
        return;
    }
    
    NSString *directoryPath = [self mediaDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:name];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
}

#pragma mark - 私有方法

/** 返回albumId对应的文件目录 */
- (NSString *)applicationDocumentsDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:albumID];
    
    return path;
}

/** 判断是否存在albumId对应的文件目录 */
- (BOOL)isExistDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的Thumbnail目录 */
- (BOOL)isExistThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self thumbnailDirectoryAtAlbumID:albumID];

    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的AspectRatioThumbnail目录 */
- (BOOL)isExistAspectRatioThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self aspectRatioThumbnailDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的Media目录 */
- (BOOL)isExistMediaDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self mediaDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

@end
